package com.citymanage.demo.controller;

import com.citymanage.demo.po.manager.Manager;
import com.citymanage.demo.po.person.Person;
import com.citymanage.demo.po.worker.Worker;
import com.citymanage.demo.service.manager.LoginManagerService;
import com.citymanage.demo.service.person.LoginPersonService;
import com.citymanage.demo.service.person.RegisterPersonService;
import com.citymanage.demo.service.worker.LoginWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@RequestMapping("/index")
@Controller
public class UserController {


    public int userCurrentID;
    @Autowired
    /*private UserService userService;*/
    private LoginManagerService loginManagerService;
    @Autowired
    private LoginPersonService loginPersonService;
    @Autowired
    private LoginWorkerService loginWorkerService;
    @Autowired
    private RegisterPersonService registerPersonService;
    @RequestMapping("")
    public String show() {
        return "index";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping(value = "/registerIn",method = RequestMethod.POST)
    public String registerIn(@RequestParam("account") String account,@RequestParam("password1") String password1,@RequestParam("password2") String password2) {
        Person person = registerPersonService.select(account, password1);
        if (person == null) {

            if (password1.equals(password2)) {

                person=registerPersonService.register(account,password1);
                return "redirect:/index";

            }

            else
                return "passwordError";
        }
        else
        return "error3";

    }
    @RequestMapping(value = "/loginIn", method = RequestMethod.POST)
    public String login(@RequestParam("account") String account, @RequestParam("password") String password, @RequestParam("gettype") String gettype, RedirectAttributes redirectAttributes) {

        /*User user = userService.getInfo(name,password);
        if (user != null) {
            return "success";
        } else {
            return "error";
        }*/

        if (gettype.equals("person")) {
            Person person = loginPersonService.getInfo(account, password);
            if (person != null) {
                userCurrentID=person.getPid();
                redirectAttributes.addFlashAttribute("userID",userCurrentID);
                return "redirect:/person";
            }
            else
                return "passwordError";
        }
        if (gettype.equals("worker")) {
            Worker worker = loginWorkerService.getInfo(account, password);
            if (worker != null) {

                userCurrentID=worker.getWid();
                redirectAttributes.addFlashAttribute("userID",userCurrentID);
                return "redirect:/worker";
            }
            else return "passwordError";
        }
        if (gettype.equals("manager")) {
            Manager manager = loginManagerService.getInfo(account, password);
            if (manager != null) {
                userCurrentID=manager.getMid();
                return "redirect:/manager";
            }
            else return "passwordError";
        }
        else
            return "error3";
    }


}






